﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Device.Location;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Windows.Devices.Geolocation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Maps.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Xna.Framework.Media;
using SMARTMobile.Common;
using SMARTMobile.Services.SMARTMobileServiceReference;
using SMARTMobile.Utilities;
using SMARTMobile.ViewModels;
using SMARTMobile.Models.UI;
using Microsoft.Phone.Maps.Toolkit;
using SmartMobile.Data;
using SMARTMobile.WinPhone;
using Telerik.Windows.Controls;
using GestureEventArgs = System.Windows.Input.GestureEventArgs;
using SMARTMobile.Models.Local;

namespace SMARTMobile.Views
{
    public partial class DuyetTTLamTuyenPage
    {
        //DuyetTTLamTuyenViewModel _viewModel;
        SmartMobileDBContext _smartMobileContext;
        private bool _isSelectedDay;
        private TCARCUST _currentCust;
        private GeoCoordinate _myCoordinate;
        private ReloadDataSmartPhoneSoapClient _proxy;
        private string _currentImageName;
        private IMAGETYPE _selectedImageType;
        private List<TCARCUSTApproval> cusApprovalList;

        private List<TCARCUST> pinCustomerList;

        public DuyetTTLamTuyenPage()
        {
            InitializeComponent();
            this._smartMobileContext = new SmartMobileDBContext(SmartMobileDBContext.ConnectionString);
            this._smartMobileContext.CreateIfNotExists();
            ////this._smartMobileContext.LogDebug = true;            
            //this.NgayListBox.ItemsSource = _viewModel.DayCollection;

            (this.DataContext as DuyetTTLamTuyenViewModel).SetMapRouteEventHandler += _viewModel_SetMapRouteEventHandler;
            //this.Loaded += (sender, args) => NhanVien.WindowSize = new Size(Application.Current.RootVisual.RenderSize.Width, double.NaN);
            //this.Loaded += (sender, args) => Ngay.WindowSize = new Size(Application.Current.RootVisual.RenderSize.Width, double.NaN);
            this.Loaded += DuyetTTLamTuyenPage_Loaded;

            //this.DoiTuongLP.ItemsSource = this._smartMobileContext.ARSALESP.ToList();
            this._proxy = new ReloadDataSmartPhoneSoapClient(GlobalInformation.Binding, GlobalInformation.Endpoint);
            _proxy.DowloadCustomerImageCompleted += this.Proxy_DowloadCustomerImageCompleted;
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            this.NgayLP.SelectedItem = GlobalInformation.CurrentDay;
            this.DoiTuongLP.SelectedItem = GlobalInformation.CurrentARSaleSP;
            string msg;
            if (NavigationContext.QueryString.TryGetValue("msg", out msg))
            {
                if (!string.IsNullOrEmpty(msg) && msg.Contains("ThayTheHinh"))
                {
                    string tempName = msg.Split(';')[1];
                    BitmapImage image = GetLastImage();
                    ShowHinhAnhCuaHang.Source = image;
                    Utilities.ImageUtilities.SaveToJpeg(image, tempName);
                    Duyet.IsOpen = true;
                    DuyetHinh.IsOpen = true;
                    this.KhachHangExpander.IsExpanded = false;
                    this.GPSExpander.IsExpanded = false;
                    this.ImageExpander.IsExpanded = true;
                }

                if (msg.Contains("IsDuyet"))
                {
                    Duyet.IsOpen = true;
                }
            }

            if (GlobalInformation.IsShowPopup)
            {
                Duyet.IsOpen = true;
                this.RefreshPinMap();
                GlobalInformation.IsShowPopup = false;
            }

            if (_currentCust == null && GlobalInformation.SelectedTCARCUS != null)
            {
                _currentCust = GlobalInformation.SelectedTCARCUS;
            }
        }

        void _viewModel_SetMapRouteEventHandler(object sender, EventArgs e)
        {
            ////this.DrawLineBetweenGeoLoc(new List<TCARCUST>());
        }

        public void DuyetTTLamTuyenPage_Loaded(object sender, RoutedEventArgs e)
        {
            //NhanVien.WindowSize = new Size(Application.Current.RootVisual.RenderSize.Width, double.NaN);
            //Ngay.WindowSize = new Size(Application.Current.RootVisual.RenderSize.Width, double.NaN);
            Duyet.WindowSize = new Size(Application.Current.RootVisual.RenderSize.Width, double.NaN);
        }

        //private void OnNhanVienClick(object sender, RoutedEventArgs e)
        //{
        //    this.NhanVien.IsOpen = true;
        //    //var saleList = this._smartMobileContext.ARSALESP.Where(x => x.Sitecode == GlobalInformation.SiteCode).Select(x => new ARSaleSPModel()
        //    var saleList = this._smartMobileContext.ARSALESP.ToList().Select(x => new ARSaleSPModel()
        //    {
        //        Key = x.Salesp_key,
        //        Name = x.Salesp_name,
        //        SiteCode = x.Sitecode,
        //        SSCode = x.SScode
        //    });

        //    //this._viewModel.SaleCollection = new ObservableCollection<ARSaleSPModel>(saleList);
        //    //this.NhanVienListBox.ItemsSource = this._viewModel.SaleCollection;
        //}

        //private void OnNgayClick(object sender, RoutedEventArgs e)
        //{
        //    this.Ngay.IsOpen = true;
        //}

        private void OnDuyetClick(object sender, RoutedEventArgs e)
        {
            this.Duyet.IsOpen = true;
        }

        private void NhanVienListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            BusyIndicator.IsRunning = true;
            this.GetCustomerList();
            BusyIndicator.IsRunning = false;
        }

        private void NgayListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            BusyIndicator.IsRunning = true;
            this._isSelectedDay = true;
            this.GetCustomerList();
            BusyIndicator.IsRunning = false;
        }

        private void ZoomIn_Clicked(object sender, RoutedEventArgs e)
        {
            if (MyMap.ZoomLevel < 20)
            {
                MyMap.ZoomLevel++;
            }
        }
        private void ZoomOut_Clicked(object sender, RoutedEventArgs e)
        {
            if (MyMap.ZoomLevel > 1)
            {
                MyMap.ZoomLevel--;
            }
        }

        ////private void DrawLineBetweenGeoLoc(List<TCARCUST> listGeo)
        private void DrawLineBetweenGeoLoc(List<TCARCUST> listGeo, Dictionary<string, DuyetStatusEnum> duyetStatus)
        {
            MyMap.Layers.Clear();
            MyMap.MapElements.Clear();

            // HACK
            //List<GeoCoordinate> listGeo = new List<GeoCoordinate>();
            //listGeo.Add(new GeoCoordinate(10.7765, 106.7010));
            //listGeo.Add(new GeoCoordinate(10.7701, 106.6976));
            //listGeo.Add(new GeoCoordinate(10.7771, 106.6905));
            MapPolyline line = new MapPolyline();
            line.StrokeColor = Colors.Red;
            line.StrokeThickness = 2;
            MapLayer mapLayer = new MapLayer();
            int index = 1;
            foreach (var geo in listGeo)
            {
                if (geo.Lat != "0" && geo.Long != "0")
                {
                    Pushpin pushpin = new Pushpin();
                    pushpin.GeoCoordinate = new GeoCoordinate(double.Parse(geo.Lat), double.Parse(geo.Long));
                    //pushpin.Background = new SolidColorBrush(Colors.Blue);
                    DuyetStatusEnum statusEnum = duyetStatus[geo.Customerkey];
                    pushpin.Background = new SolidColorBrush(GetColorByDuyetStatus(statusEnum));
                    pushpin.Tag = geo;
                    pushpin.Tap += pushpin_Tap;
                    pushpin.Content = index++;

                    MapOverlay myLocationOverlay = new MapOverlay();
                    myLocationOverlay.Content = pushpin;
                    myLocationOverlay.PositionOrigin = new Point(0.5, 0.5);
                    myLocationOverlay.GeoCoordinate = new GeoCoordinate(double.Parse(geo.Lat), double.Parse(geo.Long));
                    mapLayer.Add(myLocationOverlay);
                }
            }

            MyMap.Layers.Add(mapLayer);


            foreach (var geo in listGeo)
            {
                if (geo.Lat != "0" && geo.Long != "0")
                {
                    line.Path.Add(new GeoCoordinate(double.Parse(geo.Lat), double.Parse(geo.Long)));
                }
            }

            MyMap.MapElements.Add(line);

        }

        private Color GetColorByDuyetStatus(DuyetStatusEnum duyetStatus)
        {
            switch (duyetStatus)
            {
                case DuyetStatusEnum.ApproveAPiece:
                    {
                        return Colors.Yellow;
                    }
                case DuyetStatusEnum.NotApproved:
                    {
                        return Colors.Red;
                    }
                case DuyetStatusEnum.Approved:
                    {
                        return Colors.Green;
                    }
                default:
                    {
                        return Colors.Red;
                    }
            }
        }

        void pushpin_Tap(object sender, GestureEventArgs e)
        {
            this._currentCust = (sender as Pushpin).Tag as TCARCUST;
            this.Duyet.IsOpen = true;           
            GlobalInformation.SelectedTCARCUS = this._currentCust;
            this.KhachHangExpander.IsExpanded = false;
            this.GPSExpander.IsExpanded = false;
            this.ImageExpander.IsExpanded = false;
        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/MainPage.xaml", UriKind.Relative));
        }

        //private void NhanVienListBox_OnTap(object sender, System.Windows.Input.GestureEventArgs e)
        //{
        //    if (this.NhanVienListBox.ItemsSource.Count() == 0)
        //    {
        //        this.NhanVien.IsOpen = false;
        //    }
        //}

        private void GetCustomerList()
        {
            ARSALESP selecedSaleSP = this.DoiTuongLP.SelectedItem as ARSALESP;
            if (selecedSaleSP.Salesp_key == "00")
            {
                return;
            }

            if (selecedSaleSP.Salesp_key != null && this._isSelectedDay)
            {
                string selectedNgay = this.NgayLP.SelectedItem as string;

                ////ARSALESP selecedSaleSP = (this.DataContext as DuyetTTLamTuyenViewModel).SelectedARSaleSP;
                ////string selectedNgay = (this.DataContext as DuyetTTLamTuyenViewModel).SelectedDay;

                List<TCARCUST> cusList = new List<TCARCUST>();
                this.cusApprovalList = new List<TCARCUSTApproval>();
                GlobalInformation.TCARCUSTApprovals = new List<TCARCUSTApproval>();
                var approvalCustomer = _smartMobileContext.APPROVALCUSTOMERINFOR.ToList();
                var custWithoutApprovedList = (from tc in _smartMobileContext.TCARCUST
                                               where tc.Salespkey == selecedSaleSP.Salesp_key
                                               select new TCARCUSTApproval()
                                               {
                                                   Customer = tc,
                                               }).ToList();
                if (!custWithoutApprovedList.Any())
                {
                    return;
                }

                if (selectedNgay == Localization.AppResources.ROUTECHECK_LABEL_T2)
                {
                    cusApprovalList = custWithoutApprovedList.Where(x => x.Customer.T2 == "1").ToList();
                    cusList = cusApprovalList.Select(x => x.Customer).ToList();
                    GlobalInformation.TCARCUSTApprovals = cusApprovalList;
                }
                else if (selectedNgay == Localization.AppResources.ROUTECHECK_LABEL_T3)
                {

                    cusApprovalList = custWithoutApprovedList.Where(x => x.Customer.T3 == "1").ToList();
                    cusList = cusApprovalList.Select(x => x.Customer).ToList();
                    GlobalInformation.TCARCUSTApprovals = cusApprovalList;
                }
                else if (selectedNgay == Localization.AppResources.ROUTECHECK_LABEL_T4)
                {

                    cusApprovalList = custWithoutApprovedList.Where(x => x.Customer.T4 == "1").ToList();
                    cusList = cusApprovalList.Select(x => x.Customer).ToList();
                    GlobalInformation.TCARCUSTApprovals = cusApprovalList;
                }
                else if (selectedNgay == Localization.AppResources.ROUTECHECK_LABEL_T5)
                {

                    cusApprovalList = custWithoutApprovedList.Where(x => x.Customer.T5 == "1").ToList();
                    cusList = cusApprovalList.Select(x => x.Customer).ToList();
                    GlobalInformation.TCARCUSTApprovals = cusApprovalList;
                }
                else if (selectedNgay == Localization.AppResources.ROUTECHECK_LABEL_T6)
                {

                    cusApprovalList = custWithoutApprovedList.Where(x => x.Customer.T6 == "1").ToList();
                    cusList = cusApprovalList.Select(x => x.Customer).ToList();
                    GlobalInformation.TCARCUSTApprovals = cusApprovalList;
                }
                else if (selectedNgay == Localization.AppResources.ROUTECHECK_LABEL_T7)
                {

                    cusApprovalList = custWithoutApprovedList.Where(x => x.Customer.T7 == "1").ToList();
                    cusList = cusApprovalList.Select(x => x.Customer).ToList();
                    GlobalInformation.TCARCUSTApprovals = cusApprovalList;
                }
                else
                {

                    cusApprovalList = custWithoutApprovedList;
                    cusList = cusApprovalList.Select(x => x.Customer).ToList();
                    GlobalInformation.TCARCUSTApprovals = cusApprovalList;
                }

                if (cusList.Count() > 1)
                {
                    this.pinCustomerList = cusList.Where(x => x.Lat != "0" && x.Long != "0").ToList();
                    GlobalInformation.PinCustomerList = this.pinCustomerList;


                    if (this.pinCustomerList != null && this.pinCustomerList.Count > 0)
                    {
                        TCARCUST firstIndex = this.pinCustomerList[0];
                        MyMap.Center = new GeoCoordinate(double.Parse(firstIndex.Lat), double.Parse(firstIndex.Long));

                        Dictionary<string, DuyetStatusEnum> duyetStatusDictionaries = new Dictionary<string, DuyetStatusEnum>();
                        foreach (var cusApproval in cusApprovalList)
                        {
                            int countApproval = approvalCustomer.Where(
                                    x => x.Key == "1" && x.Customerkey == cusApproval.Customer.Customerkey).Count();
                            if (countApproval >= 3)
                            {
                                duyetStatusDictionaries.Add(cusApproval.Customer.Customerkey, DuyetStatusEnum.Approved);
                            }
                            else if (countApproval > 0 && countApproval < 3)
                            {
                                duyetStatusDictionaries.Add(cusApproval.Customer.Customerkey, DuyetStatusEnum.ApproveAPiece);
                            }
                            else
                            {
                                duyetStatusDictionaries.Add(cusApproval.Customer.Customerkey, DuyetStatusEnum.NotApproved);
                            }
                        }

                        DrawLineBetweenGeoLoc(this.pinCustomerList, duyetStatusDictionaries);
                    }
                    else
                    {
                        MessageBox.Show("Không có Khách Hàng nào trên Tuyến " + selectedNgay);
                    }
                }
                else
                {
                    MyMap.Layers.Clear();
                    MyMap.MapElements.Clear();
                }
            }            
        }

        private void RefreshPinMap()
        {
            var approvalCustomer = _smartMobileContext.APPROVALCUSTOMERINFOR.ToList();
            Dictionary<string, DuyetStatusEnum> duyetStatusDictionaries = new Dictionary<string, DuyetStatusEnum>();
            foreach (var cusApproval in GlobalInformation.TCARCUSTApprovals)
            {
                int countApproval = approvalCustomer.Where(
                        x => x.Key == "1" && x.Customerkey == cusApproval.Customer.Customerkey).Count();
                if (countApproval >= 3)
                {
                    duyetStatusDictionaries.Add(cusApproval.Customer.Customerkey, DuyetStatusEnum.Approved);
                }
                else if (countApproval > 0 && countApproval < 3)
                {
                    duyetStatusDictionaries.Add(cusApproval.Customer.Customerkey, DuyetStatusEnum.ApproveAPiece);
                }
                else
                {
                    duyetStatusDictionaries.Add(cusApproval.Customer.Customerkey, DuyetStatusEnum.NotApproved);
                }
            }

            DrawLineBetweenGeoLoc(GlobalInformation.PinCustomerList, duyetStatusDictionaries);
        }

        private void GPSExpander_OnExpandedStateChanged(object sender, ExpandedStateChangedEventArgs e)
        {
            if (this.GPSExpander.IsExpanded)
            {
                this.KhachHangExpander.IsExpanded = false;
                this.ImageExpander.IsExpanded = false;
            }
        }

        private void KhachHangExpander_OnExpandedStateChanged(object sender, ExpandedStateChangedEventArgs e)
        {
            if (this.KhachHangExpander.IsExpanded)
            {
                this.GPSExpander.IsExpanded = false;
                this.ImageExpander.IsExpanded = false;
            }
        }

        private void ImageExpander_OnExpandedStateChanged(object sender, ExpandedStateChangedEventArgs e)
        {
            if (this.ImageExpander.IsExpanded)
            {
                this.KhachHangExpander.IsExpanded = false;
                this.GPSExpander.IsExpanded = false;
                this.listButton.ItemsSource = _smartMobileContext.IMAGETYPE.ToList();
            }
        }

        private void ExitExpander_OnTap(object sender, GestureEventArgs e)
        {
            this.Duyet.IsOpen = false;
        }

        private void DuyetKhachHang_Clicked(object sender, GestureEventArgs e)
        {
            GlobalInformation.CurrentARSaleSP = this.DoiTuongLP.SelectedItem as ARSALESP;
            GlobalInformation.CurrentDay = this.NgayLP.SelectedItem as string;
            //GlobalInformation.SelectedTCARCUS = this._currentCust;
            NavigationService.Navigate(new Uri("/Views/CapNhatKhachHangPage.xaml?msg=" + "IsDuyet", UriKind.Relative));
        }

        private void DuyetGPS_Clicked(object sender, GestureEventArgs e)
        {
            this.BusyIndicator.IsRunning = true;
            ////var currentCust = _smartMobileContext.TCARCUST.FirstOrDefault(x => x.Customerkey == this._currentCust.Customerkey);
            ////currentCust.Active = "1";
            ////_smartMobileContext.SubmitChanges();
            var currentApprovalcustomerinfor = _smartMobileContext.APPROVALCUSTOMERINFOR.FirstOrDefault(x => x.Customerkey == this._currentCust.Customerkey && x.Type == "GPS");
            if (currentApprovalcustomerinfor == null)
            {
                APPROVALCUSTOMERINFOR approvalcustomerinfor = new APPROVALCUSTOMERINFOR();
                approvalcustomerinfor.ID_PK = Guid.NewGuid();
                approvalcustomerinfor.Customerkey = this._currentCust.Customerkey;
                approvalcustomerinfor.Key = "1";
                approvalcustomerinfor.Type = "GPS";
                _smartMobileContext.APPROVALCUSTOMERINFOR.InsertOnSubmit(approvalcustomerinfor);
            }
            else
            {
                currentApprovalcustomerinfor.Key = "1";
                currentApprovalcustomerinfor.Type = "GPS";
            }

            _smartMobileContext.SubmitChanges();
            this.RefreshPinMap();
            MessageBox.Show("Hoàn thành duyệt GPS");
            this.BusyIndicator.IsRunning = false;
        }

        private void ThayTheGPS_Clicked(object sender, GestureEventArgs e)
        {
            ThayTheGPS(true);
        }

        private void ThayTheHinh_Clicked(object sender, GestureEventArgs e)
        {
            ThayTheHinh(true);
        }

        private async void ThayTheHinh(bool isGps)
        {
            Geolocator geolocator = new Geolocator();
            try
            {
                var asyncResult = geolocator.GetGeopositionAsync();
                var task = asyncResult.AsTask();
                // add a race condition - task vs timeout task
                // hardcode to 30s
                var readyTask = await Task.WhenAny(task, Task.Delay(30000));
                if (readyTask != task) // timeout wins
                    throw new TimeoutException();

                if (geolocator.LocationStatus == PositionStatus.Disabled)
                {
                    // Couldn't get current location - location might be disabled in settings
                    MessageBox.Show(
                        "Chức năng định vị (Location) chưa được Bật. Vui lòng Bật chức năng định vị để sử dụng Bản Đồ.");
                    BusyIndicator.IsRunning = false;
                    isGps = false;
                }
                else
                {
                    // position found within timeout
                    Geoposition currentPosition = await task;
                    _myCoordinate = new GeoCoordinate(currentPosition.Coordinate.Latitude, currentPosition.Coordinate.Longitude);
                }
            }
            catch (TimeoutException ex)
            {
                MessageBox.Show("Lấy thông tin GPS tự động quá lâu. Vui lòng chọn Vị Trí lần nữa");
                BusyIndicator.IsRunning = false;
                return;
            }

            if (!isGps)
            {
                isGps = await Windows.System.Launcher.LaunchUriAsync(new Uri("ms-settings-location:"));
                ThayTheHinh(true);
                return;
            }

            var currentCust = _smartMobileContext.TCARCUST.FirstOrDefault(x => x.Customerkey == this._currentCust.Customerkey);

            if (distance(double.Parse(currentCust.Lat), double.Parse(currentCust.Long), _myCoordinate.Latitude, _myCoordinate.Longitude, 'K') * 1000 < 20)
            {
                GlobalInformation.StateImageType = 0;
                NavigationService.Navigate(new Uri("/Views/ChupHinhCuaHangPage.xaml?msg=" + "DuyetTTLamTuyen;" + _currentImageName, UriKind.Relative));
            }
            else
            {
                MessageBox.Show("Không đúng tọa độ");
            }
        }

        private double distance(double lat1, double lon1, double lat2, double lon2, char unit)
        {
            double theta = lon1 - lon2;
            double dist = Math.Sin(deg2rad(lat1)) * Math.Sin(deg2rad(lat2)) + Math.Cos(deg2rad(lat1)) * Math.Cos(deg2rad(lat2)) * Math.Cos(deg2rad(theta));
            dist = Math.Acos(dist);
            dist = rad2deg(dist);

            dist = dist * 60 * 1.1515;
            if (unit == 'K')
            {
                dist = dist * 1.609344;
            }
            else if (unit == 'N')
            {
                dist = dist * 0.8684;
            }
            return (dist);
        }

        private double deg2rad(double deg)
        {
            return (deg * Math.PI / 180.0);
        }

        private double rad2deg(double rad)
        {
            return (rad / Math.PI * 180.0);
        }

        private void KhongDuyetGPS_Clicked(object sender, GestureEventArgs e)
        {
            this.BusyIndicator.IsRunning = true;
            var currentCust = _smartMobileContext.TCARCUST.FirstOrDefault(x => x.Customerkey == this._currentCust.Customerkey);
            currentCust.Lat = "0";
            currentCust.Long = "0";

            //GPS khi thay đổi cập nhật Shopvisit
            string dateNow = DateTime.Now.ToShortDateString();
            Shopvisit currentShopvisit = _smartMobileContext.Shopvisit.FirstOrDefault(
                               x => x.CustomerKey == this._currentCust.Customerkey && x.DateVisit == dateNow);
            if (currentShopvisit == null)
            {
                currentShopvisit = new Shopvisit
                {
                    CustomerKey = currentCust.Customerkey,
                    Lat = currentCust.Lat,
                    Long = currentCust.Long,
                    DateVisit = dateNow,
                    ID_PK = Guid.NewGuid(),
                    StartTime = DateTime.Now.ToString(),
                    CMode = "MCP"
                };

                _smartMobileContext.Shopvisit.InsertOnSubmit(currentShopvisit);
            }
            else
            {
                currentShopvisit.Lat = currentCust.Lat;
                currentShopvisit.Long = currentCust.Long;
                currentShopvisit.DateVisit = dateNow;
                currentShopvisit.StartTime = DateTime.Now.ToString();
                currentShopvisit.CMode = "MCP";
            }

            var currentApprovalcustomerinfor = _smartMobileContext.APPROVALCUSTOMERINFOR.FirstOrDefault(x => x.Customerkey == this._currentCust.Customerkey && x.Type == "GPS");
            if (currentApprovalcustomerinfor == null)
            {
                APPROVALCUSTOMERINFOR approvalcustomerinfor = new APPROVALCUSTOMERINFOR();
                approvalcustomerinfor.ID_PK = Guid.NewGuid();
                approvalcustomerinfor.Customerkey = this._currentCust.Customerkey;
                approvalcustomerinfor.Key = "3";
                approvalcustomerinfor.Type = "GPS";
                _smartMobileContext.APPROVALCUSTOMERINFOR.InsertOnSubmit(approvalcustomerinfor);
            }
            else
            {
                currentApprovalcustomerinfor.Customerkey = this._currentCust.Customerkey;
                currentApprovalcustomerinfor.Key = "3";
                currentApprovalcustomerinfor.Type = "GPS";
            }

            _smartMobileContext.SubmitChanges();
            MessageBox.Show("Hoàn thành xóa GPS");
            this.BusyIndicator.IsRunning = false;
        }

        private void DuyetHinh_Clicked(object sender, GestureEventArgs e)
        {
            this.BusyIndicator.IsRunning = true;


            var currentApprovalcustomerinfor = _smartMobileContext.APPROVALCUSTOMERINFOR.FirstOrDefault(x => x.Customerkey == this._currentCust.Customerkey && x.Type == "IMG");
            if (currentApprovalcustomerinfor == null)
            {
                APPROVALCUSTOMERINFOR approvalcustomerinfor = new APPROVALCUSTOMERINFOR();
                approvalcustomerinfor.ID_PK = Guid.NewGuid();
                approvalcustomerinfor.Customerkey = this._currentCust.Customerkey;
                approvalcustomerinfor.Key = "1";
                approvalcustomerinfor.Type = "IMG";
                approvalcustomerinfor.Comment = CommentImage.Text;
                _smartMobileContext.APPROVALCUSTOMERINFOR.InsertOnSubmit(approvalcustomerinfor);
            }
            else
            {
                currentApprovalcustomerinfor.Key = "1";
                currentApprovalcustomerinfor.Type = "IMG";
            }

            _smartMobileContext.SubmitChanges();
            this.RefreshPinMap();
            MessageBox.Show("Hoàn thành duyệt hình ảnh");
            this.BusyIndicator.IsRunning = false;
        }

        private void KhongDuyetHinh_Clicked(object sender, GestureEventArgs e)
        {
            this.BusyIndicator.IsRunning = true;
            var currentCust = _smartMobileContext.TCARCUST.FirstOrDefault(x => x.Customerkey == this._currentCust.Customerkey);

            string dateNow = DateTime.Now.ToShortDateString();
            IMAGECUSTOMER imageCustomer = _smartMobileContext.IMAGECUSTOMER.FirstOrDefault(x => x.CustomerKey == this._currentCust.Customerkey);
            if (imageCustomer == null)
            {
                imageCustomer = new IMAGECUSTOMER()
                {
                    CustomerKey = currentCust.Customerkey,
                    IsApproval = 0
                };

                _smartMobileContext.IMAGECUSTOMER.InsertOnSubmit(imageCustomer);
            }
            else
            {
                imageCustomer.IsApproval = 0;
            }

            var currentApprovalcustomerinfor = _smartMobileContext.APPROVALCUSTOMERINFOR.FirstOrDefault(x => x.Customerkey == this._currentCust.Customerkey && x.Type == "IMG");
            if (currentApprovalcustomerinfor == null)
            {
                APPROVALCUSTOMERINFOR approvalcustomerinfor = new APPROVALCUSTOMERINFOR();
                approvalcustomerinfor.ID_PK = Guid.NewGuid();
                approvalcustomerinfor.Customerkey = this._currentCust.Customerkey;
                approvalcustomerinfor.Key = "3";
                approvalcustomerinfor.Type = "IMG";
                _smartMobileContext.APPROVALCUSTOMERINFOR.InsertOnSubmit(approvalcustomerinfor);
            }
            else
            {
                currentApprovalcustomerinfor.Customerkey = this._currentCust.Customerkey;
                currentApprovalcustomerinfor.Key = "3";
                currentApprovalcustomerinfor.Type = "IMG";
            }

            _smartMobileContext.SubmitChanges();
            MessageBox.Show("Hoàn thành xóa hình ảnh");
            this.BusyIndicator.IsRunning = false;
        }

        private async void ThayTheGPS(bool isGps)
        {
            Geolocator geolocator = new Geolocator();
            try
            {
                var asyncResult = geolocator.GetGeopositionAsync();
                var task = asyncResult.AsTask();

                // add a race condition - task vs timeout task
                // hardcode to 30s
                var readyTask = await Task.WhenAny(task, Task.Delay(30000));
                if (readyTask != task) // timeout wins
                    throw new TimeoutException();

                if (geolocator.LocationStatus == PositionStatus.Disabled)
                {
                    // Couldn't get current location - location might be disabled in settings
                    MessageBox.Show(
                        "Chức năng định vị (Location) chưa được Bật. Vui lòng Bật chức năng định vị để sử dụng Bản Đồ.");
                    BusyIndicator.IsRunning = false;
                    isGps = false;
                }
                else
                {
                    // position found within timeout
                    Geoposition currentPosition = await task;
                    _myCoordinate = new GeoCoordinate(currentPosition.Coordinate.Latitude, currentPosition.Coordinate.Longitude);
                }
            }
            catch (TimeoutException ex)
            {
                MessageBox.Show("Lấy thông tin GPS tự động quá lâu. Vui lòng chọn Vị Trí lần nữa");
                BusyIndicator.IsRunning = false;
                return;
            }

            if (!isGps)
            {
                isGps = await Windows.System.Launcher.LaunchUriAsync(new Uri("ms-settings-location:"));
                ThayTheGPS(true);
                return;
            }

            var currentCust = _smartMobileContext.TCARCUST.FirstOrDefault(x => x.Customerkey == this._currentCust.Customerkey);
            currentCust.Lat = _myCoordinate.Latitude.ToString();
            currentCust.Long = _myCoordinate.Longitude.ToString();

            // GPS khi thay đổi cập nhật Shopvisit
            string dateNow = DateTime.Now.ToShortDateString();
            Shopvisit currentShopvisit = _smartMobileContext.Shopvisit.FirstOrDefault(
                               x => x.CustomerKey == this._currentCust.Customerkey && x.DateVisit == dateNow);
            if (currentShopvisit == null)
            {
                currentShopvisit = new Shopvisit
                {
                    CustomerKey = currentCust.Customerkey,
                    Lat = currentCust.Lat,
                    Long = currentCust.Long,
                    DateVisit = dateNow,
                    ID_PK = Guid.NewGuid(),
                    StartTime = DateTime.Now.ToString(),
                    CMode = "MCP"
                };

                _smartMobileContext.Shopvisit.InsertOnSubmit(currentShopvisit);
            }
            else
            {
                currentShopvisit.Lat = currentCust.Lat;
                currentShopvisit.Long = currentCust.Long;
                currentShopvisit.DateVisit = dateNow;
                currentShopvisit.StartTime = DateTime.Now.ToString();
                currentShopvisit.CMode = "MCP";
            }

            var currentApprovalcustomerinfor = _smartMobileContext.APPROVALCUSTOMERINFOR.FirstOrDefault(x => x.Customerkey == this._currentCust.Customerkey && x.Type == "GPS");
            if (currentApprovalcustomerinfor == null)
            {
                APPROVALCUSTOMERINFOR approvalcustomerinfor = new APPROVALCUSTOMERINFOR();
                approvalcustomerinfor.ID_PK = Guid.NewGuid();
                approvalcustomerinfor.Customerkey = this._currentCust.Customerkey;
                approvalcustomerinfor.Key = "2";
                approvalcustomerinfor.Type = "GPS";
                _smartMobileContext.APPROVALCUSTOMERINFOR.InsertOnSubmit(approvalcustomerinfor);
            }
            else
            {
                currentApprovalcustomerinfor.Customerkey = this._currentCust.Customerkey;
                currentApprovalcustomerinfor.Key = "2";
                currentApprovalcustomerinfor.Type = "GPS";
            }

            _smartMobileContext.SubmitChanges();

            MessageBox.Show("Hoàn thành thay đổi GPS");
            BusyIndicator.IsRunning = false;
        }

        private void DuyetImageButton_Click(object sender, RoutedEventArgs e)
        {
            ////ImageExpanderSelectionChanged            
        }

        private void ListButton_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string test = sender.ToString();
        }

        private void SelectedImageType_OnClick(object sender, RoutedEventArgs e)
        {
            var vm = (this.DataContext as DuyetTTLamTuyenViewModel);
            if (vm.IsNetworkAvailable)
            {
                if (e.OriginalSource is Button)
                {
                    if ((e.OriginalSource as Button).Tag != null)
                    {
                        IMAGETYPE imagetype = ((e.OriginalSource as Button).Tag as IMAGETYPE);
                        _selectedImageType = imagetype;
                        _currentImageName = GlobalInformation.SelectedTCARCUS.Customerkey + '~' +
                                            imagetype.ID.ToString() + ".jpeg";
                        if (ImageUtilities.CheckFileExist(_currentImageName))
                        {
                            ShowHinhAnhCuaHang.Source = ImageUtilities.LoadImageFromIsolatedStorage(_currentImageName);
                            DuyetHinh.IsOpen = true;
                        }
                        else
                        {
                            _proxy.DowloadCustomerImageAsync(GlobalInformation.ArSaleSPType.Sitecode,
                                GlobalInformation.SelectedTCARCUS.Customerkey, imagetype.ID);
                        }
                    }
                }
            }
            else
            {
                MessageBox.Show("Kiểm tra kết nối mạng");
            }

        }
        void Proxy_DowloadCustomerImageCompleted(object sender, DowloadCustomerImageCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                DuyetHinh.IsOpen = true;
                if (e.Result != "-1")
                {
                    //string image = @"0xFFD8FFE000104A46494600010101006000600000FFDB00430001010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101FFDB00430101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101FFC00011080096009603012200021101031101FFC4001F0000010501010101010100000000000000000102030405060708090A0BFFC400B5100002010303020403050504040000017D01020300041105122131410613516107227114328191A1082342B1C11552D1F02433627282090A161718191A25262728292A3435363738393A434445464748494A535455565758595A636465666768696A737475767778797A838485868788898A92939495969798999AA2A3A4A5A6A7A8A9AAB2B3B4B5B6B7B8B9BAC2C3C4C5C6C7C8C9CAD2D3D4D5D6D7D8D9DAE1E2E3E4E5E6E7E8E9EAF1F2F3F4F5F6F7F8F9FAFFC4001F0100030101010101010101010000000000000102030405060708090A0BFFC400B51100020102040403040705040400010277000102031104052131061241510761711322328108144291A1B1C109233352F0156272D10A162434E125F11718191A262728292A35363738393A434445464748494A535455565758595A636465666768696A737475767778797A82838485868788898A92939495969798999AA2A3A4A5A6A7A8A9AAB2B3B4B5B6B7B8B9BAC2C3C4C5C6C7C8C9CAD2D3D4D5D6D7D8D9DAE2E3E4E5E6E7E8E9EAF2F3F4F5F6F7F8F9FAFFDA000C03010002110311003F00F8424B146822B686490CEE6E8064B6B88E49975196237118164AD0402E23610A972D3DA9F3E391D254B7893F1C7F6A7B1169F167C550B4CE665934D9CC7757111B59D1FC3FA4F950C534102F950B32858E60CD1C52B5E4B24443061FB6DF646995ADE585E31328FB3BC46F1E460ECD66628616B678E4965D35277B440ED24F3FEEE1919639123FC5CFDAB4A9F8C1E29749244C3787E4581E7325B88E7F0F68C5209DD63DC1122916194B456F2221B7511046B5298D293E66D7A5D3E9A3D7FAFF0033C28A6A4B4FEB55F8FF005E7F1269B3C8DAD5D4EA4ACA2DADDE064DDB8CB15EDCBCC918531065BA71B63E0FDA13ED6320924761A3C5321473214334532968E3313348ADE53B79C86E96595B0B1450602C886588889257B76E7EC20964D47526592186490C24C825BD0CE933DC4E268608EDA7133AAFD989656BAC34E43C4484866E919127B8923B88951ADE60CCD0491F9B144086FDD4AC90C455E49E465F2C1730288A322786D92E7BA94DBF775B3B27D9F9BFC6FA6DE7A86217B8DDAED24ADD764D75BE9F9DEDA9D3EB0B3BCF32FDA9AFDD1EC9D9ADAE22B98E40F04929737B1C5653CB35B8B95B79120578E336CF01999363CD8697122AC88B35B48D132BEE7B85BC92E19677B9DA255B89B1E7B4D692B43E6EC09033286B97FB4CDD27886F2DAE6F2EAE516E66FB55EBF9774C902CBE658410C6CD34FA72DB5BDCA0977C91A42AD15DC6609AD669EE25332F0CB736D14B1295BA0A116448DEDB68B854958C4CECB1032C6A2575497ED0ADF67924952297CF14D371935D747B2B59EC92E8B4D766DDF4479518FB91935AB4BA6CDA8BD3D775D7A9E83A1EA1A6DA693A84F7115A5E6B973A9E8D1DA5BDE219E6363E56A4924B6E89A50F26686FA1D3A6961B8D4230DB555348D66DE5BD6B1F76F06784BE256A1E1FF0CEA3A47ECF3AEF8D348F126A71FC3ED2F5B9BC29E36961F12F88351D615746F0CF87EFBC3F269D732F8C27F125D5A594096F35EEB97F2489E0F944DA3B9D12BE5DD02FD2DB56D32F2E74F8B53820BDD3661A46A8DA8456FA9C70BA5C1D3278F49D434ED44DAEACC424E349D5AD752837CCD0DE69D7531783ED4F845A878F7C4BF07F53F0649FB65D9FC27F02CDE2DB6D1B5AF817E2FF001D7C70D1BC31A969FAFF00883C25A75D78E4E8DE16F0D6B9E05D6F42B7BBD5AFB54F191911BC4F67A078435EBF1A0EA4D2E85A7EA19E26ACBD9256D22D59A7AC55AD2E5515CC9BD1A69A4AEDFD956C2AE129D792F69CD6F79DE339C3DEE5E55A4251E6F75DADBAB464AF2516798DDDF783E6D6DAF753F851ABE8B657563696FF00D81E14F196B1A259D8EA16B6765A60D42CF51F16E85E36D467D327D4E397539E39646BB9AF753C8D50E9F6F664D2F116A5F0FF0057BBB5B9F0FF00812E3C182D746BA8351B1B0F175E6AFA35EEB1FD9F610E9DAADAC1AFDAEA5AAE9B6F6F742EAF752D3BFE122BD8EEEDAEB4DB483FB2A4D3DE6D4FEDCFD9FBC31FB43783FC5F37C40F859FB5AFC11F84FE33F0BEB37BE12D1356D6BF688F06F86478BE4D1B4DD4FC713E91657B797F2E8575F0EFC44F6BA2E8A755F88B2787FE1A788F58F15695A47892FC58E91F10A7F0B7A9E93A8FEDFBFB427C5C5BD6F067C37FDA3BE23687A6DE787A4F007897E1BFC06F14C56B2F81F48F16D8D8EADE39F8377DA769D25A78CFC14936BF742E3E23F8562F135EB5ABF867C7F69AB6856D79E191E4D5AB38C5B69DB74DD59A4F569DF9A2D2D1BD6EEF24AED5B9A3950CB6851AB1A909D57250B462EBD6A94ECEEDF342556516927293BA76718DADCB75F947AB5B7C38B7D2740BEB2D53C7567E2A92CAFADBC47A6DE68BA25EE8D35C45AAF88A5D27FB1357B5D5AC353BBB17D2478561D5A1BBD0AC2E06AB71AB6AB65732436D67A6CD7EFF00C3FF000424D1B52BDD33E2EF8B2CF5BB289AE6CF48F127C246D16C752960BB8160B15D4BC3BF103C6D25BCD7315D4D2417175A4FD863834DBDB6D44E9D28D2D753F69FDA7754F8D5FD8FE1FF00087C60F869E05F04CFA1CDA9EAFA17883C35F04FE15FC39D675EB6BFD13C3E6DA093C65F0D7C21E1FB7F1CD8D858CBA76BB15FDD5F6AD728FE249753D46F2E65D5AD643F17B806E151E48CC1E7A24A64457306F2AF6CB33C6E17C9DB18713462404AB34A250EAB07461A729D357B2B5AEE3CB2BA7AABB704FED595D69CA96CECF9F139754F6D271C56269BBDF95C70F24AEE3271942AE1E738AE8973BB47693BEBE916FE00F08DE69D7B7767F1ABC0367A8D9CBE0FB7D3BC2FE20D1FC7FA46ABE201AC6A53E9DADC56F7FA6F8075ED0ED6DFC2D7863BA8A4D7F5AD2EDB55D1EEAD357B6992E6DB54D1ADFD1FC65FB2DEABE12BD86EB44F8FBFB2F7C41D3AE2CBC4F78355F05FC6EF0E2DB5A4FE0ED42F74FBDD2E6D0BC531784BC6B1DF6B1776A61F063BF85A3D335EB29F45D5FC3F7F3E87749AD27CBA6230C0D15C42EBE67EE0B052A594C8DE4ADC5B94F2EDEE4A46A198C2D0EE12288EDD64DF2D1112CA8D0CE2283ECF379770A55D6429E7452BC6D96B6668E531991C2DBC842AAC50EDF35A4B7D27196EB47A5EF66AD65DACDB7AB4EFD6DCBA58F42845D38463293A8D3D64D28B7B7F228C559DECD2D9DB5695BE8DF0D7EC9BF1FF00C7961AF6B5F0FF00C12DF112DBC23A7F8775AF11BF80BC4DE1AF195D68FA27896DEFAF34A92F93C39AB6AD736DB21D37503A85834765A87877C958B50B6B112599BEF36F187C15F8B9F0FF0043D2BC4FE34F86BE36F0A783F5C8ECA1F0BF8A7C45E11D774AD0F5C37DA73EB1652E8FACEA3610E8FAEC7A968A6E2F6CA5D3AF9E0B9B058EFAD6392D95648BCF5C335D4324C63856E255896DD9CC71CC86DC891B6C2AE628A211348F24F6C63552CF2858A554123DE5F0B48F4B92EAEA5D25E79EE134FBBBFBA8ACAD75448120374968035A4970D1C48B7F2342B2DC2D9A3C93E6277AE673AD7F7A51E5BBB2517AAE977CCD37DDDBCAC9EAB64F569B5AEAB4D969E7AECDDFD15BA90CC9BEED639199AD96F278A144291B27D9A24BC31B0776951C848D637DD21956CE3B580B92ED2AD9C22466863B8F2EE5556DDD0CA862B898BA449B91A04CFD9E4309731F94242D2C88F180A5248AD2447600B9B7967816595624DCD1148E4F2D2DD26562518ABDBDC18E08A58832142E2748F7AD6CC2B22AA5C29791A2553E61DD12DD216F294FEE0188C81236691D9E1481A389EDA74431525BDAEAF6EFF7F6F2D3F30BDAD6F4B2BDADD6F6E8471C511558E79163F2432176587664C8DE58CCD6D3DBEE92250FFEA926255CA394320256FC56314902986282627AC59785E2C34A240D70C6749A456C098C31AA4A5A291A49240492B0755A76ED6E917D9EEF5E88693EDF84BCBB74FF0083E47F4833A349F6A9DAEA4F22DE4FF45B6BDF2F74D2FF00ABFDD896626562A36188DC4D047736D6C76CB22DB63F10FF006B97D9F16FC6C563692137FA55BCF379E4469249A0E9B0CAB09493CA8E6322BC824723CA8660B395324AEDFB5D796572B6A2783500CE4456D70B2B4B3CD3968E5D82E2D7ECEB22440C4CCC914EF0DDBCE8D219434D227E1E7ED637B7579F173C7E03DA4B3C5716764D3A1794C42D34CB28628A7912448A454710C722158C32C50B280EAA24F529ABBF3DBEFB3DADF8DCF61E8DAFC6C9E89E9BFF005BF73E58F09429777DE2296D9A5814BE84A02EE40B98EE2605A369A479A698F96F7314855FED01953CB2CD2DBF5B7517D92FA184B3DAB47210CC56298C5122CCAC1650B1CB212C1A19EE26923F325F3A6F2ADD1D08A3E048BED136AD733177937598F39E2909936D94FBE416E238A01B55A28E4486251022AC20C47696EBAFAD671A859011C53299A358B199A22EB04372B2430C7E6289258A58648A28EE5864B4EC51D1D64E9A2AF512EFA7CDB497E2658D76A4DDB549BE8F66B4ECEF7D35B5FAF5347C59A70B2D39357B91736D1DF7893C4D6312DDDBD9D85C3B69DA4F876EA6B992C74A32DA4605BEB50496FF006696286549A478B104B6EF67E51724471CB3B2232CB32A5C40AC9042DE586826F2D4985925244A4B178E758D44B219248DA01F5278C742B7B7FD9B3E156BA9E4C17BAA7C7DFDA134C958CA5098B42F02FECC97B690BAF9B246BFD9EDE22B98E14CCE8F70EB32B193CB44F96A7F30379E0B335B5E44C0188DC25BC9033958A4824491E6478E78AEAF21DB72D1821B04988268DDE4DF9BDFB26D257B2E9AFE3D8F3E9A8BA50B689C29E966ADFBBA6DA6BBDEE9F9A76D0D5D18C979ACE8DA7DBDF69F6735C6AB6314771A9C7726C2D85ECFE6477BA838B59196C6C94BC8D1C704D1B24770D736F7525B25BC7EF317C18B8B1D6B5AD3A3F893F0E6EEEF47D1F5AD49352D0B5CD63C410EA136870E877D3E9B6D3687E1DD4FECD35FDBEB315BD96B5E22B5D1BC17E641A9A5EF89B4F36F2DB2F85784FC51E29F0778C3C2DE3AF0A6A2FA578B3C1DADE89E29F0CEA82DAD27BAD27C41E1BD42CF54D1F54862BFB7BED3AEDAC2EAD2D35154BB867B368544C6036E974B71EDBF0EB42F873AB786AF53C4B7FE25D27C6177ACCE74A9ECEE743B8F0B5AE8F069F019BFB634DD460B6BFB6177793497D6BADD8EAB7C235F0D4DA1C1E1ABE1E288FC45E18C71389A586C2D59D45AA578CDDFDCB5B9AE9349AE54EF7D6ED35B59FCFE7D8CA997E165898FB671A719A9468E1A589926E2F92AFB384A339284D294A29DA715CBEEB9A9D3F53F869FB327C64F8AD1C13FC39D23C2BE2DBD1717B6C34BB0F88FF0D60D66286D34D9F5D93559BC39ABF8DAD3C4F67A20B48E46B5D42FF4C8ED6EAFE26D1DA55D716EEC570B4CF81BF17BC4BAD4DA3F86BC17A9EB5AAA06962D2B48B9B3BBBFD5618AE345B77BDF0B0B7BC913C51A581ABD96A90EADE1D9B59D30E82D7FE279659F40D2753D5AC3E9BF10FECFBFB3DE8D6D67A7EA7E31F8CFE07D635AF1841AA0D47C4FE0ED3BC4317877E0CEA33789974D37DE16F0F25A596BDE3B68B4FD33C4497BA37C42B4F0CEB567078A749FEC9B23A0E93AAF88B98D6FF66CD22FAF3494F875AEFC71F1F787AFACEDACF5AF1449FB3E6AFA759785B5282EBC5116A1666C749F1CF8D26D42DAD74CB6F875E22B399A0B3D62F740F1AEA3713681A16A1A068F61E23F0E9E6385AB3841D6A509546E30E79A8455A4A2DCA539A845295949C9C52BC5BE5BC4F8BCBB88F32C73AF3A78AA54A10A6E6A9E2F21C7E167F0CA36A539664E9E2A4AAA8B70A09CB92F0D1CA155FCF9E0FF00D9FBE3FF00C49BFB4D17C07F05FE2BF8F756D6F446F16E9BA3F877E1C78CFC43AADDF85EC2EED61BCF106976BA7E8D75A95F787ED27D5B4C827D52DADEF2D229355B7F3E491AF6067F2EF167C39F15F813C41AA683E34F0AF887C0FE27D38A26ABE16F15687A8F87756D3E2B87BAFB1DB5FE9FAEAD86A1662496D4B4075386E6775FB5380238B70FB07E155BFC46F83DAE7893E207C37FDA63C4BF007C4BA6697AF6847C6DA14FF1E3E1EEB3E2BBAD2EEA3D4B52F87FA2F88FE1C785751D6A3D4750D39FC11E206D2FC56BE1BB58AD7C4F6D1EA96DA749A75A19FBED06DFE2FF00C7AD4FE206B9E20FDB77C32DE23BFB08BC57E2B9FE337C58F8C9A67897C753C1E06BDF00DEB4DAC6BDE17D4342F1678A34DF08F8ABC51F0C8E993F892EB5BD47423E23D03C336DAC78475017575D0F150A4DC6338CA374B994D495EE9349C79A3D1D9DECF4B68F4FA4C37116095A8E2AB38D78C942A4E182C5D2C3294A7184231A95235295E52767FBF935752B72BD3F39B55F0AEB7A46A57DA3EB369A9E8DACE877C74BF10699A95ACB61A9699AAC52DCDBCBA75FD85C5A8D46D2F2DAED6F61BC86F765DDA5C5B5CDBDC794914A1301AC9A2B6944D045134924C40961755F2DA2593729891364C81DD518DC7FA4C8C778313491DBFEE57823E297FC1423F6645F1EFC56F87DFB75781E13E1CB5F15A3C761FB5D7C29F8B5A97C438F4AF16E9336B56DA37C31D6FC5DE3CBFD6A5D7D353BEF1969DA8788FC2BA25878A742B3D6DEDF50D4F5E4D37C3B7BF9F5E29FDA7BE3478B344F881E18F155FF00C3AD5B4AF891E2BD47C61E31D4352F833F05478C6EBC45A84F6325CA58FC451E00FF0084D7C336107D9638ACFC27E1BF1168DE1EB2B28EE34ED2F4CB2B490C3210C57B649C1C7954ACDB94B6D2CEDECF7B34AD77AB7AA6AEFE9633936BF95F2D9A57D2C977577DBDEB6D7691F18A6970BC818851E4C379E6468E8B1A9E9BE58DDA4562B6DE6A4B28F2502932A7EF1D4C19B3D9AF9AB25D468D2CAB9C38957722AEE8250A1656440E5E390B235B4611D7F70A1913EDEF147ED35AE7887C05A7780EEBE17FECDF6B6765E13D6BC3373E28D33F67EF85FA4F8DB5F7D73C4175E20BBF19EADE34D3BC3D65AD7FC25D6D6634DF0CE8BA96877FA1D8E95E1FD02C8697A743E209FC43AE7893E64F11EA336B3AC6B9AB4FA7693A441E20D4B52D4E4D1343B48B49D1F477BFBBBABA3A7695A65B836FA7E956D2036765650EEB7B28D63B58607484BB0A7295D3564AFB4935D95B44ED6B3D631EAAD7B9BFF5F82BBDDDB5D37E8B7E9C5410234F35C279F384B950BE52615DCDC427742A7CEB66468D63895E1955E289522999A1D824DA821862479C4AC6759ED84722F92EEE4C4859D36C92B2AC91877123342019247670914314D62CACA443F7444639217B87812589A39ED901112C66795BCC9248645F296057D8CB1871224A5B520B268A58A62F691A185079B0C90B5C2C7B8B90A72DBA38E3F2D923961588C2F7312792717126551A4FE493F5FF0086B6DD03FA4490C645BAC125ACD71224B32B18DAEA760229A511B0315F5B3796D14D1AC63718D151488D1A5DA856E259346A8B29DC026C8DA28E52E5902F9FB9104BB506F87698C08DDCC8CCF23140857239ABEAF5EB757EDE4FFC8DA31692D7F15DD69FD776F63FA0DD566B75B18888AC35156904D3C96F24A8B636B70590325BC103BDA849992196E272F01921B68F708A36F33F03BF6876B8D4BE2678EE5313247278BFC491C9700468F8B6BD9AD57E48A59E382E0A011C92794B04CED2CF7091466079BF7BAFC4CCF2A3DCB69D25DD9DC8B8B64854FDBAD2431DB35ADCC3E6DE3EEF2EE266994BDCC8018A386D8B6C793F06FF00687BD947C45F1E44CF0242FE33F10437719B668CB48DAC5F6238564C49218CC9E4289DA491D6DA4121731EC97E969C6CDE96E96B5B7FEBF13D094AD24D757777E976BB3E9AD99E59F0C6C26960D767768E3896E74E81E5991658302396564991A4BC60A934EAA818A711C6F6A0348F21EAF5BB39C6B93C31450DAC905CC01FCF69FC86631C6DE4ED0E26918DCCD2C3E4C132BE2E004F21251193E0D59C37763AFCA6496063AF2C7101BD58DA47A7DA991638989667494CD136D8D5BCE8A28DC12FE63F7B65142BF11B4169E64B0822D73405BCBA3A94FE1EB1B0B4BC9F4F92499F5ED2AC6F6FB448AD2067B83AC5959DFDC5AAA477B65697D3ADB4545276AC96F76BBDAC9ABAD5AD2DBBBF95ECAE4E31A787A8FB424F47AA765D5F5F5D0EA7E23DBA5C7ECCFF000AB448EC6D56EED7F68EFDAA1678E212DB43656F2F817F655B38F4C8229A04BAB7301D36F2DD04F6F05C4DE4CF1C80B472C6DF28369F1BCF14F7312A0792E9DCCDE588EF62F2CC81CCAB6F22A40CA66F32416EC9E4B35C8902092393E94F893A8EA138BBF0DDF6A369AB4BA4FC4AF88DE20B9D5B4CF125CF8AF49D46F75C3E1AD26E2FF4DF114D717A75FB2D525F0D6F875EF3EFEE359896DAFBFB4278EE6190F8F269515CCDA75ACD6F1B179EDE2B8490359C9042D2DBA18639A68A582388C6F2126602DA3C2DC3832BCC91DC9FEF64B5ECBA5FAABA4DEB67E7AF9EA70615F361A9CB76D2B6AA5ADA31566959DECB6567B2D2C657C3FF0086DE2BF8A5E32F05FC31F0169EBAE78EFC7FE2CF0A7813C1FA24975A7D91D67C55E32D4ECB43F0FD80BDD465B0D2ED25BBD6B56D36CE4BBBAB9B482CF797BAD4224B79857D05F097C43F0F2C3C2BA6D878B7C6FE27D367B2F1A3DEDBE93A77C3AF0A78F74ED2B4AD5F4DD174EF126B9A7FF6EF8C3C39F68F102E9BA61F2743BCD25746D5974AD12E26D7B426469EC3D7344FD973F68DF1FF00822D7C09E10F87BFDB3A6782FF00E167FC5DB2FECBF1AE93AAEB3A8E993CBF047C0DE2C5D13C247C737DE1BBAD661F1037C3BF0E5A697E02F0D47F137C65E2ABDB8F07DD5FF8DAEBC15A5E89E0AF33D07F665F8F1E239F451E15F83DF12FC4575ADDA7866F347B3D13C03E30D6E7D56DFC79E2B5F00F816EB4C86C34AB817D67E36F1A5C2784FC2BA869DE7D97887C5535BE81A52DF6B372BA7C518DC3CA17A55A9CA128B49C66A509C64ED64D3E59A6FA6CDA6BB9E066188C8B1FECF058CC4E5D5FEB0E13C3E1EAE2683956739BA14A5461ED39AAB9D553A50953BA751B846F2D0FD3BB2F8CFF00B1543E1CF1D59E9B7DFB3B78C74C3F05AFF41F0CEADF17BF638F18FC1EF8D367E32F10B7872C6CE0F04E93FB3E7C58F1F7C2B9751F0E1F00683E2B6F89BF163E21DD5FE99AC78EFC7BA1E9FE1EF19F8566B8F08F88FC13C69FB557ECE307C1FD0FC1DF0BBF633F871E10F8A77FE1D9740D6BE35C9F1AFF00693D7FC573691378366F05DEDCDF783EDFC73E13F04D978FAEFC5969E20F1C4F716DA7EB7F0C2F3C3FAC785FC1B2FC3AB98740D69B54FCF0D5F43D5FC3FABEA7E1CD574DBBD135DD12F2EED352D1350B6B8D235AD3757D32E6E20D474BD4B4FBA11DEE9BAA5BCF091A8DADDD9453C1776A20BB85180B539F3C32C52867B6116D896E1879A1967B7F2D6699554C6E923967685B0649583B37EF628667BAF0DE5587728D495E692768B97B8F9B95DE7056536B955AE9D92BA5D5EF87CAF0787A30C351C3C6950A7CDC9497338A739AA926B9A52936E5ADE5294A376A2D5D9FA33F0B7F6BAFD9A3C3FF000DFC35E04F8B7FB21788FE2FEBD69E28F11DDF8BBE2647FB56FC63F871E2BD6F40F10685A9FDAB4DF0E7862C46BDE00F0B6BD7DE2D1E1CD77C49E20D4FC2BE35B7F15E8DE0DD23C35A9F87575B9D3C736DCFF867F68CFD98742D02FADADFF676F8AEFE2F4BBF8B32E87E3AFF0086AAD674793C26FE3FF1178260F87AFA0E8DA2FC2DD36CA49BE147C3AF075D786E47D567B94F885E21F16EA3E28F152C5A0697E0EF01E87F05DAAC9E64491ADB340B22C823887909318D2468DF74D0AC8C162659374F6F22C4D24714F11F29AE13505BDB2A1B99957CC3F6869DE6772F04BBB63B967762E63CEFC794585C41E4640DCD354B058751694631D79B952B26F55AABBBAF79E8D38DD45D938C5AEA597D04F4A3496ADB4A952517CCACD4928DA69C5B4D4934F4D3DD56FBD6FBE3E7ECE97BF053C7DE0D7F0D7ED40FE3FD7B54D04DB4DAE7C62F841E35F87D0E8735E68D71E3A4D42CB55FD9D2D3C6D6FAFEAB3DD78CF54F0DEABE1EF12E9567A7EA1E26D2F55D4B47D667F0F6A83C55C35F687FB006B9E16D11346F12FED61E13F1647F113C71A6F892E6FBC13F077C7DA06A1F086E35807E1E7883C37F66F1B7C2BD4B4FF883A0E83049A778DBC1BA949AC787FC5DAFC0DAC681E3AF0269BB34687E4F5B55532C5244C8D1B4926E1023A8768A79646DA93FCB70B1441030649230A1A5695151199E543348C408D54867936B8C91340F31695632622ECD1BA891D5D532D10136D31CD10C3469DF92CAFD6C97A5D4524EDAA5CC9D96DBB3A634630F7A318A7D5462A29E918ABA8A8C5B4A29276BAB68FB7AF78DFC0DFB3FDBF892E6C3E1F7C6CD7756D1ADFE15E9DE34B0D7BC59F0CB58D0631F122E7C37A0F8A759F83F7BFD99AA6B3AAC2FE1D375E27F00597C438340B8F0DF88FE29E87A1C977A5F84BE176BF71F13BC31E5DF133C33F0E748F11DD597C2BF1BEB1E3BD02DA25DDAFEBBE105F00DFEA57491A497770BE194F12F8EBCBD32131CB069B35C6AB67A95F5AC6BA8EA1A568E6E61B6ACD781648DA6894C71EE8CC865DC215588E7CFF90409B642CCB0431913346D70B14CCE84D38D83CB705B31413CF20918B9DF71B86D57440162663B264B68E5F2D5AE312798E81220C59C1DDD494ADCDAB6BED356BDA2BE15A452B6FAF332D41DE2DDB4DD77DB756DD5ADD3777B9CB43639238C155F250488CC02CA93CA2378279954621769239D58B3488F1B9991A656D1B4D3E69A136F1A380D1CB24615A4C2990C611C2307B568D2D1A5F2CA945037C92ED309C74FFD9F1E1123B699653B65592E6556766512C865F2E458EE0866559B7050CA915C2C28B0F952C768D824858C2A92302E9B102B44C646459E132BAABF9CAB2246A9BDE2917728F2FF00793A73D59DF67AFE2BBB7BEAFF00AD8724934EDA7E8ADFD7E1D35C5B6B4920334CD23BEE6456906E85C492F993345BBE62C9180101921323EDC6F4119590AEA174D2CEC26BA6B79D002F95B9672D2F2FBDADA4955CED4853F76FB5447994077482DCAE573D744ADEABCBFAFC7A3345AA47EE2BC0C11A74533AAA5F4971732349BADE56BC8A182598426665961919D22658374D24D6D1218E69406FC0FF008EF35DC9F133E2089640C53C7BE2792EA0B796E66730DB6B9A9C72C6A247DDF6849D7FD2A270D83B16DE1540FE5FEF3EA2A9B5DCC56EEB1CD1A3BC69E45EDE4B02BC02E191D5A2B716D342D1CD33C8DF698628BEC8B1ADB30B4FC1CF8D2CF71F15FC6F74A6DE7997C65E24785DE676BB9211AE5EBB48EA3F792DDABBB4A9E7C8AF2086520A0F2C0FB083D1EBADFBF97F56F99D72B292F95F7E8FEEFBBCC9BE05E98B75A4EAB34AB14CEDE2930C4EEF2811C31E9BA2CAC2694869D64B569A77485BCD7C832388F7CD24DDD3453C1F14746D42DE496D6EAC35CD06EF4DD474ED5EC3C3B2DB5E5A5D6957D6535BEBDA85A5E69BA2CD6B2461EDF5ABDB4934DD19E11A85CC33C5652D9DC33F671B13FF0008CEA45EDEE6DE38FC71771795B6E27555FECFD2594ED13C913B2CD1A2CC8199495863585DCDB997D1E0D27EDBF1B340B1821BCBABABDF1B7856D6DF4DB44D2E1BEBB3A85CE8904961A7BF88A01A22EA5769710DAC32EB914BA51BB091EB2B2699279673A73B62237DF65B793BE8ADA5ED7B6FAEA89C62B612BC95B4A536EEEC95937AB4D3B5B57AAB774ECCF11F17C3757DE25D76E9C4237EAB7B246B0DF68BA84492DDEA7A92DE5AE9F71E1DB2D2344D4ED5E17B385AFF004282DF4BB98A68AFAC2CAD6D196CC63F862DEE2DB5CD32EA13A57DBAD356B0BDD3E3D5A3D2A7D09EE22BE8D219355835E2DA0DC582CECA6FA4F10A8D29EDEFAEE2D58BC2D3DDCBF447C52F0D68FA56B5E2096CEFAE52EA3F889F1034C4F07EB9636769E32D334BD0B50D2C6892F89DB49D274FF0442FA8FF00685DE9B6F1785F52D521B6D53C37AD5BBE91A068EFE1DBED6FCCAC2C6E21996EECEE0C37705DDACD67750CD347E4496E619636B478C3B2DC473A7DA364D240EB249108198A301AD495EB5D7471575FDD493FC9F4DB43970A97D5285B54E8D36AD2E65EF4636B49DEE969695DDD2BDDEEFB6F863E04F82FE26B2BAD4BE25FC6BD63E1A6B73F8A9F4C4D3B46F855A8F8F92C3C37FD90F752EBD79ADA788FC3D08866D4FCDF0FD9E8D6F05FDE2DF4D05DCAA9A5CD3DD69F2697E0DF0A5978CF58B4F0E7C7BF0C697A6E87E3EB1D3FC37E35D6BC31F13F439F54D05BC5E349B2F88AFA6F84BC35E33D5BC349A3E9905B78DB57D357ED5E25B3B089AD3C2B61E21F11416FA31E020D0A4501731B2878A3B697CB019E40CF3C85764862DF0CE19441048B12A32B084496AEB36DE9FA32B121E245498C4C81D2E822984AC6E12059372B471F989FBD0CF239B659014CC09BD7A8AAD0853F614E32837FBD8AA9ED269DAD195EA3A768FC4B969C65D1C9C743CEA797C638BAB89A988AB5F9B95D3A15A18574B0EE3AA95174F0D4F109B7ABF6B5EAABA4D24FDE3DAAF7E1A786BE20EA5A878B7C6BFB50784F52F1FF008CAE6F7C63E31F1478DB4EF8E5AC9D4FC4DE27F0B2F8E7514D73C5365F0F3C59E34F12FC417F19DE6B1E11F1FDEDF787469C7C52F61E22D13C65E3CF0C6B5AB78A7C374FF686FD9934AF8183C1F73A6FC7BF805F1D6C3C67A4C7AFDA5C7C16F17789BC4377A4E957DA1689A9D8DE78A348F14782FC27ACF846F2FEE75BB8D20F853C59A7683E37D2B59D0F5B8B5DF0BE8F6377A05D6A9C847A5EC8E37580233496EA2410C66792E0CE54B6E2A92452B6D79088FCA6B678CC8A506D5686E3499655930B71739516ED1BCE1776D8E28A258CA48B2AEC3693ACAD14921B6117122279CEBE5B8492D2D65FE6BD34B5D2D1B5A3D6DEF7A2AC95B4BDF7BEAF7B269DF6564AD6DB5BBBDFC4D2C2386783CF4431C423430C866468A35492795D1DBF78239508223DBB2421CCA2395439B1FD98D22C8C992AA24461232DB22472ACBBD488D621036E77814A472BA34AF127911C6EEFE8973A1B451F9A628E54B669E40104A62996147DED70FC2C91848D6190A98DA40C09936365A78341092A27950C29218D964B2955119220BF6608256B652C22F3A38D5550796912DBB485648E3C1C6576DAFBBFAB969697D1EB6B3F35A3FEBF0B5CE085806DCEB6F8C4AC650222A1195DCC72C42004C1722378E46C3B4D146E6E0B4723441E716A2393698D5A149E35C831A3C8921448C344F24023D90CF6E24700B32CD76B0451C0A8D1FA2C7A199406753B914B801FE5DEAE4F98D71E6AC4B6DE582249CDB4EF306569562400D3D747578C7950E1CDCB924C46499DA73123BAC65D883E5BB22218DA477F2C0548E3060CE4EC996A0BAB7F2FF863CF22B201E40CEA17F7690282D94124917973158A3694C6F23C61B2E865985BC91959A48B328B205DD25766B84402668E3602491E3457FDF990317FDF091E499ADE4315AF112A1610FA20D3A58EE7FD0E28DB31C62457891A4DEC54298E3977C6B282CD8962B79232EC9BA3FDDCD14D987498A17900E93465E2F261779251F67937448C7CD586796410A990C92C4C843F9F13C6AF171CDBBD9F4DBFCC9E47E4FF00AFEBEE30068FFBF554B84B62CEC98B81BC48A6491DE568DA53E53BC5246B1796649479F36D9960454135BE992C2A661E54AFF6590BC6A925B9669CB33491C0D0702695BCA0ECCB2AC8A9E603B9CAF46D6C4CAD1CD1DB33026757F3788DAE1158CB26E48F6ADC445A1914C7101732B23A6EB799AAE7D819B2E15370B9918B2068CA48D34656790C72C81831F29A42A86757321F2A507647CF36AF7BF4D48953725B35FD2E9BFF00573997B1B991CC51491E01795A359140323158DE47FBE5B0B1C622214C615E448CA22869CAEA12DED6C955A4370CB83197821406426491E26DC25137945448E8B2221577903EF9016A2B9B9D2D34DEFD3BA7DBFADFA14A127D5AF2D3BAEFAF9FCBD0FD7E169665E4921C08ADAC9209EE85A8F323768A72C4AE1DD648665B813ABF9EB72D74B1C890222BDB7E08FC74B4B65F8A9F11E4F39653178E7C596F6F34856344F2F5FBD2311A34496F6F1797FBD89A2662C5C33B2CB2FDA3F7F1BED704507D9818E462D72861976490CB01F35678D6E90432CD2C8818C112C50B4724533C772D14B29FC26F8E315C5C7C4CF886F1B5C4DE678D3C4F20BB6299475F105E1B55244EB70D3B3BA662B88774732DCAF96B2AAB49F694A2DB76B7CFBFF4FA9B54B732D766BF4D3CF7DEDA6DA23BFF00D97F4C91BC33A8BB80FBBC768A46E4125D19346F0E02D2245221726DA3DB189D9D19012C21759A53D86BFA3D95D7C565B7D5ACF54D4F4CB8D67C3D6D7D69A4CD141AEDE594B6DA62DFDB69125FDA5CDA457B7168CB0693F6BB79F6CF2426EACEE02BDAB697ECA7A04977E1F9AC6D628D6E6F3E20F90B6CCE01954695E158A0B44B838137972A6761FB247B8471AED02215EABF12FE166BFE12F8F16FE04F136997F6DE208B5CF0BE9F7965A169565E2DD6525D674ED06F6DE1D3343B7BA4D1B5FBC92D2FEDE5B1D25359823D46E1EDB489AEECD9D36E094562630725CED73462DA52714E317A36B44DA4DDD2BB5B5CE8AD42BBCBEB62230A8A8D3E5A53C44613746956AB4EA4E9427562B9635670A15A74E9F373CA34AACA116A9CED85F17BC29F07348D13C29A0780AC7E2C697F143C3DE2BF8B3E1FF8ADA57C458BC213787F4AB7B1F12269BF0F6DBC212E896DA7EAF6FABC5E1FB7B8D3BE2459EBB69F664F13E956DAA7872EE4D1F579748F0EDDF00E91F0AF45F865ACEBFE39F0E5C6ADA96BB7FE24F09784B5093C0BE24D4ADB44BD4B7F044DABF891B5ED2FE2FF00C36D16F75BF08E97A9DEDF785FC2977A2F8D34CB8D775310FC40B3B4D2FC47A06ADA773DE3CD325BAF16F8856E2CA4B246F14F88A31A23F8793C2BFD9B0B6A92A35B47E1F8B50BF8FC3F25B98923974B8750D457484B77B58EF669912E6BA7F835A6F854FC55F85E3C7BADE99E13F87F2FC48F04FF00C265E24D6FC21ABF8EF49F0EF859BC4D60DE29D7F53F00E8B730EAFE37D2F48D38DEDDDC785347BA8F55F115942FA2E9B776D7779118BB68CA9C317175231953F6BEF2A8A728F2F32B73FB29466D256E6E4776AE92BD8F1E54AAD6CBE9469E22BD3AAE850A9ED70DECA35672E48D4708FD6A15A318D46F95F3FBCA2EDED13BC8F54F1AFC3CFD987C61E0397C51E17F8C9A1FC3EF1A694359D3ACBE177FC29BF8C767A3F8AEC749F175E5BE83E23D73C717BF107E30AE97E27F11F836E93539747D36C53C2FA05E5859684D790DD4FA8F88A6C2F853F037E16F8CA596D3C63FB44FC3EF8657171E17B9D6B43B9D6FC0FF00167C41A0278B22D4FC2D2DBF863C69A8783FE1FEB5E20D05EF3C3BA9F8B2FAC7C41E12F0BFC42D3EDBC51E0FB5F0E5DC761A46BF69E2BD3781BAD02F2FA1B6F1341E184D3F409AE6DB43B3BBD3E0D4E3D02F756D0F4AB14BDB6B7D47519F588A7D726B7B8875DD6ECA2BD387D4566B7D3346D2DB44B25FA553F64DF8FDA4F86749F16EABF0AFC696FA06AD0F862FB49D6A7D2E6B6B092D3C73E1E6F1778364BA64F322593C4FE18B2B8D73453711ACDA96951DC6A56CB24103CCBEF7D5E38C5ECB0D81A4BF7718C2A51FAEB6F92AD3BD78C6B62269CAA7F0E49D3F669556A34E15545C7CEA4AA51719D5C7E26AB553DA54A55965EDC54E8CE2B0D3787C2D39725194BDA271A8AACA7422A55AAD2756153B5F037EC53F0E3C73E1AF1678D6C3F6CBFD997C21A4F8493C592CBA2FC4FD53E207863C7BE288BC11A2F832EB5AD43C2BE10B0F875E26BCD5ED75ED6FC616FA57C3BD2AEF54B2F1CF8E6D747D5F59B3F04D8DFF00873C63A3786BC8FC09FB327877C7FAF695A60F8FBF017C15A56B106BC6CBC45F10757F1CF86742B5D434C3A439D135AB483C037BAD68F3EBDA6EB973A9E95AD5C6910F859D347D434D97C423C46969A3DD7471FC08F89ABA87F679F0078C06A56F70F61FD9E3C39AECB78BA8C33790D66E90594E8F78B3BA5942B0991CC993E5BAED8561FF00850DF140C6CF0FC37F1A08D83CF095F096B72C577F669AF2069C186CAEECDA14BBB7BF81B724AAF3C1B259414736DC18ACA6B61211A95E94E10959294E1C909DDA69735A2AF249AF75A4D3765A269D4C0E6B9E62551C931D8EA35E34AA3FABE0B0385CC399C535EDA746AE1EAE23969CE70738C2AC22D2516E329393DAF057EC30DF1024F89D6BA27ED1BFB26DA6A3F0CB50D6349874CF17FC75F0EFC35B3F886BA66ADAC699A5EA3F0DF5AF89B6BE13F0BF8A74BF11C5A1DCEA1A15F45E21B78F4ED1EFB46BAF10C7A17F6ADAC6FF003AF83BE05F89FC6177771E8977E0DF2ACA6F09C57979E21F891F0D7C2F6D62FE31F1069DA1E8BAA4F77E28F186956D6B650EA1AA5843E2BD5649EEED3C111DC2DE78FAEFC336B697D7763EDD73FB3AFC62DF7B047F0A7E23196196582E08F067896268AE2270651281670B4B744898B44BBA69A6FB3DC18B33ED8B2AEBE047C4DD19DA5BEF865E3CD35D55049F68F09EBF6B1C6E0E15DDDECE05B78ADD983BBCA913EE46491A3F20089C327ACE8CAACB0B5DC6716E9B842714AEB47AD3A8A706DC5B49A6D2B29ABDD75BC4283A3496261CF4E518D67563173A9CAA1CE9A84E92A5564937751718C9DDD2718F29AFE0AFD877E31FC42F86D0FC58D02EBE06C3E044D66CF45BAD43C63FB507ECD7E01D4F4AD63519FC64DA1E91AEE81E37F8B1E1AD7BC37AD6BF69E0CF146ABE12D175EB0B1BFF0014681A0EA5AEE8715EE976971730F0127ECA5F15E0BAD5F498B43D0575BB6F147877C2D6BA141E3BF87726B5AFEA7E33B4D1B52F03EA1E0CF0F43E285D43C6FE13F1AE91ADE95E23F08F8DFC1D67AEF82BC4BE1DBBB6F11E89AEEADA02DDDD42D9FC2FAB40274B8B7BEB69E2B961751DC5A4D1AF9D148CDC0DF2889A289E50B1CD248E8D663F73712C8638B1E7D06496E9EDA3B39CC28AEC248A091D444DE58540E1DA558E649764927CE650EBE6324CF0CADF353961E955946B46728AE64E14E6A94F992B2F7A54EAA5696E9D36DA6D5D3D4F72584C5D6A117875C951A8B8CAAE1EA54824ED77C909D16F9AF7BAA96D9EA777E25FD88FF689F098F872DAB7C3BBF79BE297C30B2F8DDF0FB45D2BC43E12F11EBDE26F86579E197F17D978E61F0D78635ED5759B6D053C269AD7881F50D674AD26E6C6C7C33E367BD3687C19E31FEC4F9DBC7DF0BFC61F0EFC57AD7843C7BE19F10F80FC61A04A96BAE785BC41A35D787BC43A46A1222DE8B5D5B44BBB7B7D46D2EFEC77569751C1756F1CB6F2080AC56D26D64EFEEB44BB00385955E6F94B8323CD26F500AC4852129212248C2B88D84853C892312AB572779A7DAED78AE9565658BF77771AC9E53243931DCAA905527454768DDE39264D836452DBAC465F1AACD3764FADB57D92D17CEEEDFAA35961A508FBD092D7ED26B5B2BA4DA4F5D1FCEC79ABDAB1DC6530C902DC3348B28B748CEC7B78499258D5E24B84B34892E923651184203344B1C62F3D9B463ED0EBB658E4591D15A0C2ACA375D46C63F3256323BBF983CD52CC5E4965125C448FBAB670F9A774265964B8866669997ED1F28DEDE6B2CECEEEBFBE7579238A23210A161504BA1B24137932445F096DB21B85DE8D20FB4450CCCAA55A18E5053CDFE113F96E60485E39A4E7728DBBF959983A6AFD5797FC39CD25B22AB3C4F6F1379922C805B24D179BE6C8242C2E6FE14170FB10C9B1B82AC02005B0574F0E9CD24852E115A4D8D3326C4906657550CC16462F2622059980F25248D32DE61DA562D45EE93F54BFAE9F80D41766FEFF00D3D51FAAFF0061053CE8AD7E4321992DA71279672EEB2CB3492C7736C4348D8F2D5E6B7B849126DE88AD669F867F196CA35F88FE3A10ADBC4F73E2FF00144D99FECE5500D5A5C6F9D66C88612E8F09B38CB849238E672D75224BFBC973645AD9560371F6A996786078EE558153344438966BC960C2BDC25B5ADC5CD9B35C099200B1C8CAAFF8A1F182CD878CBC5B1A87694F8AB5B265918C6E5D355BF9D6492DCC4238BCC5936EC909282E1010CB3F94FF007F4293BB4D3D7F3EBE9D1EA736224935ADFD3FAF5F2E87A7FECB36B043E18BB6B6891643E379E3430E59E13FD95E1B48DE31299D479F6AA91078A26DDFBC5536F3B4AEDE8F776F22FC65D28E93A54BAA5EC1E2AF0CB69FA4DAF852CFC6B3DF4CA74716F610782F5799745F16A5E010DBC5E16D5EF22D375EBA64D3B51DB6D79718E7FF0065CD31E4F0C6A56FF2BAAF8DE7F93C850EF23E97E1BB96625713F95B5B7B2EE52E4C9BA208A59BD0F59D32EAE7E2379365A70F114F7FAC5941069290DE4D2EB17D28B1365A6B5A787A6B0D4AE4EA17890D941FD937716A0C6E1FEC205CC8B2C39CE9DAB293D3BDBAED6DB5BF55DB4D8CAAD597B09ABFD876924DB5A6FCBAB977B6EF6470FF00104";

                    byte[] imageBytes = System.Convert.FromBase64String(e.Result);
                    BitmapImage img = new BitmapImage();
                    img.SetSource(new MemoryStream(imageBytes));

                    ShowHinhAnhCuaHang.Source = img;
                }
            }
            else
            {
                if (_selectedImageType != null)
                {
                    _proxy.DowloadCustomerImageAsync(GlobalInformation.ArSaleSPType.Sitecode,
                              GlobalInformation.SelectedTCARCUS.Customerkey, _selectedImageType.ID);
                }
            }
        }

        private void ImageButtonExit_Tap(object sender, GestureEventArgs e)
        {
            this.DuyetHinh.IsOpen = false;
        }

        public BitmapImage GetLastImage()
        {
            var imageList = new ObviousCollection<Image>();
            using (var library = new MediaLibrary())
            {
                Picture photoFromLibrary = library.Pictures[library.Pictures.Count - 1];
                BitmapImage latestBitmapImage = new BitmapImage();
                latestBitmapImage.SetSource(photoFromLibrary.GetImage());

                return latestBitmapImage;
            }
        }
    }
}